import 'dart:io';

import 'package:extended_image/extended_image.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class AppImage {
  static Image getIconByPath(String path,
      {String? package,
      double? width,
      double? height,
      BoxFit? fit,
      Color? color}) {
    return Image.asset(
      AssetImage('assets/icons/$path.png').assetName,
      package: package,
      color: color,
      fit: fit ?? BoxFit.cover,
      width: width,
      height: height,
      gaplessPlayback: true,
    );
  }

  static AssetImage getAssetImage(
    String path, {
    String? package,
  }) {
    return AssetImage('assets/icons/$path.png');
  }

  static DecorationImage getLoadProvider(String? url,
      {BoxFit fit = BoxFit.cover}) {
    return DecorationImage(
        image: ExtendedNetworkImageProvider(
          url ?? '',
        ),
        fit: fit);
  }

  static Widget getLoad(String? url,
      {int urlWidth = 0,
      double? width,
      double? height,
      bool color = true,
      bool file = false,
      ExtendedImageMode? mode,
      BoxFit fit = BoxFit.cover}) {
    if (url != null && url.isNotEmpty) {
      if (!url.contains('http')) {
        return ExtendedImage.file(
          File(url),
          width: width,
          height: height,
          fit: fit,
          enableLoadState: true,
          loadStateChanged: ((ExtendedImageState state) {
            switch (state.extendedImageLoadState) {
              case LoadState.loading:
                if (state.loadingProgress == null) {
                  return Container(
                    color: const Color(0xff3E2E65),
                  );
                }
                return Container(
                  color: color ? const Color(0xff40285C) : Colors.transparent,
                  child: Center(
                    child: CircularProgressIndicator(
                        valueColor: AlwaysStoppedAnimation<Color>(color
                            ? const Color(0xff251142)
                            : const Color(0xff362056)),
                        value: state.loadingProgress!.expectedTotalBytes != null
                            ? state.loadingProgress!.cumulativeBytesLoaded /
                                state.loadingProgress!.expectedTotalBytes!
                            : null),
                  ),
                );
              case LoadState.completed:
                return state.completedWidget;
              default:
                return Container(
                  color: const Color(0xff3E2E65),
                );
            }
          }),
        );
      } else {
        return ExtendedImage.network(
          url,
          width: width,
          height: height,
          fit: fit,
          enableLoadState: true,
          mode: mode ?? ExtendedImageMode.none,
          handleLoadingProgress: true,
          loadStateChanged: ((ExtendedImageState state) {
            switch (state.extendedImageLoadState) {
              case LoadState.completed:
                return state.completedWidget;
              default:
                return const SizedBox();
            }
          }),
        );
      }
    } else {
      return Container(
        color: Color(0xff3E2E65),
      );
    }
  }

  // getIconByPath
}
